// ==========================================================================
// 云捷GO自动生成model扩展代码列表、增、删，改、查、导出，只生成一次，按需修改,再次生成不会覆盖.
// 生成日期：2021-08-01 08:34:56
// 生成路径: app/model/module/project/project.go
// 生成人：gfcms
// ==========================================================================
package project

import (
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/os/gtime"
	"myblog/app/utils/page"
)

//新增页面请求参数
type AddReq struct {
	Name      string      `p:"name" v:"required#项目名称不能为空"`
	Number    string      `p:"number" `
	Type      string         `p:"type" `
	Manager   string      `p:"manager" `
	Customer  string      `p:"customer" `
	Email     string      `p:"email" `
	Telephone string      `p:"telephone" `
	Ordertime *gtime.Time `p:"ordertime" `
	Overtime  *gtime.Time `p:"overtime" `
}

//修改页面请求参数
type EditReq struct {
	Id        int64       `p:"id" v:"required#主键ID不能为空"`
	Name      string      `p:"name" v:"required#项目名称不能为空"`
	Number    string      `p:"number" `
	Type      string         `p:"type" `
	Manager   string      `p:"manager" `
	Customer  string      `p:"customer" `
	Email     string      `p:"email" `
	Telephone string      `p:"telephone" `
	Ordertime *gtime.Time `p:"ordertime" `
	Overtime  *gtime.Time `p:"overtime" `
}

//分页请求参数 
type SelectPageReq struct {
	Name          string      `p:"name"`          //项目名称
	Number        string      `p:"number"`        //合同编号
	Ordertime     *gtime.Time `p:"ordertime"`     //接单时间
	Overtime      *gtime.Time `p:"overtime"`      //完结时间
	BeginTime     string      `p:"beginTime"`     //开始时间
	EndTime       string      `p:"endTime"`       //结束时间
	PageNum       int         `p:"pageNum"`       //当前页码
	PageSize      int         `p:"pageSize"`      //每页数
	OrderByColumn string      `p:"orderByColumn"` //排序字段
	IsAsc         string      `p:"isAsc"`         //排序方式
}

//根据条件分页查询数据
func SelectListByPage(param *SelectPageReq) ([]Entity, *page.Paging, error) {
	db, err := gdb.Instance()

	if err != nil {
		return nil, nil, gerror.New("获取数据库连接失败")
	}

	model := db.Model("t_project t")

	if param != nil {

		if param.Name != "" {
			model.Where("t.name like ?", "%"+param.Name+"%")
		}

		if param.Number != "" {
			model.Where("t.number like ?", "%"+param.Number+"%")
		}

		if param.BeginTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') >= date_format(?,'%y%m%d') ", param.BeginTime)
		}

		if param.EndTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') <= date_format(?,'%y%m%d') ", param.EndTime)
		}
	}

	total, err := model.Count()

	if err != nil {
		return nil, nil, gerror.New("读取行数失败")
	}

	page := page.CreatePaging(param.PageNum, param.PageSize, total)

	model.Limit(page.StartNum, page.Pagesize)

	if param.OrderByColumn != "" {
		model.Order(param.OrderByColumn + " " + param.IsAsc)
	}

	var result []Entity
	model.Structs(&result)
	return result, page, nil
}

// 导出excel
func SelectListExport(param *SelectPageReq) (gdb.Result, error) {
	db, err := gdb.Instance()

	if err != nil {
		return nil, gerror.New("获取数据库连接失败")
	}

	model := db.Model("t_project t")

	if param != nil {

		if param.Name != "" {
			model.Where("t.name like ?", "%"+param.Name+"%")
		}

		if param.Number != "" {
			model.Where("t.number like ?", "%"+param.Number+"%")
		}

		if param.BeginTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') >= date_format(?,'%y%m%d') ", param.BeginTime)
		}

		if param.EndTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') <= date_format(?,'%y%m%d') ", param.EndTime)
		}
	}

	//"","项目名称","合同编号","项目类型","商务","客户","邮箱","电话","接单时间","完结时间","","",
	model.Fields(" t.id ,t.name ,t.number ,t.type ,t.manager ,t.customer ,t.email ,t.telephone ,t.ordertime ,t.overtime ,t.create_time ,t.update_time")

	result, _ := model.All()
	return result, nil
}

//获取所有数据
func SelectListAll(param *SelectPageReq) ([]Entity, error) {
	db, err := gdb.Instance()

	if err != nil {
		return nil, gerror.New("获取数据库连接失败")
	}

	model := db.Model("t_project t")

	if param != nil {

		if param.Name != "" {
			model.Where("t.name like ?", "%"+param.Name+"%")
		}

		if param.Number != "" {
			model.Where("t.number like ?", "%"+param.Number+"%")
		}

		if param.BeginTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') >= date_format(?,'%y%m%d') ", param.BeginTime)
		}

		if param.EndTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') <= date_format(?,'%y%m%d') ", param.EndTime)
		}
	}

	var result []Entity
	model.Structs(&result)
	return result, nil
}
